'use strict';

exports.index = async ctx => {
  const { query } = ctx.request;
  const loginService = ctx.app.weapp.LoginService.create(
    ctx.request,
    ctx.response
  );
  const checkedData = await loginService.check();
  const { openId } = checkedData.userInfo;
  console.log('participated openId', openId);
  const { userId } = await ctx.model.WechatAccount.findByPk(openId);
  console.log('exports.index = async ctx userId', userId);
  let pageSize = 10;
  if (query.pageSize) pageSize = Number(query.pageSize);
  const where = {};
  if (userId) where.userId = userId;
  const options = {
    limit: pageSize,
    offset: query.currentPage ? pageSize * (query.currentPage - 1) : 0,
    where,
  };
  console.log('model.Store.findAndCountAll options', options);
  const { count: total, rows: list } = await ctx.model.BalanceRecord.findAndCountAll(
    options
  );
  ctx.body = {
    list,
    pagination: {
      total,
      pageSize,
      current: parseInt(query.currentPage, 10) || 1,
    },
  };
};
